const { JSDOM } = require('jsdom')
const fs = require('fs')
const { FormatHandler } = require('./format-handler')
const { htmlPath, total } = require('./helper')
const axios = require('axios')

exports.parse = async function parse () {
    console.log("正在解析HTML")

    for (let i = 0; i < total; i++) {
        const page = i + 1
        const path = htmlPath(page)
        if (!fs.existsSync(path)) {
            console.error(`page:${page},文件不存在：${html}`)
            return;
        }
        const html = fs.readFileSync(path, {
            encoding: "utf-8"
        }).replace(/\s{2,}/g, "").replace(/\r\n/g, "").replace(/\n/g, "").replace(/\<style.*\>[a-zA-Z0-9:\,\.#;\s\{\}%\-\*\/]+\<\/style\>/g, "")
        if (!html) {
            console.error(`page:${page},文件为空：${html}`)
            return;
        }
        const dom = new JSDOM(html, { contentType: "text/html" })

        const list = Array.from(dom.window.document.querySelectorAll('#innerTable tr'))
        // list.forEach((tr, index) => {
        //     if (index === 0) return;
        //     const [star, ext, signature, desc] = Array.from(tr.querySelectorAll('td')).map(td => td.textContent)
        //     if (!/^\w+$/ig.test(ext)) return;
        //     FormatHandler.add(ext, signature, desc)
        // })

        for(let m=0;m<list.length;m++){
            if (m === 0) continue;
            const tr = list[m]
            const [star, ext, signature, desc] = Array.from(tr.querySelectorAll('td')).map(td => td.textContent)
            if (!/^\w+$/ig.test(ext)) continue;

            const signal_link = tr.querySelectorAll('td')[2].querySelector('a')
            const detail_url =  'https://filesignatures.net'+signal_link.href

            console.log(`正在获取 ${signature} 的offset`);
            
            const {data:detail_html} = await axios.get(detail_url)

            const detail_dom = new JSDOM(detail_html, { contentType: "text/html" })

            const detail_trs = detail_dom.window.document.querySelectorAll('#innerTable tbody tr')

            let offset = 0

            if(detail_trs[3]){
                const detail_tr = detail_trs[3]
                const detail_tds = detail_tr.querySelectorAll('td')
                const detail_td = detail_tds[3]
                offset = detail_td.innerHTML.replace(/\s/g,'').replace(/\&nbsp;|\<br\>/g,'').replace(/.+\:(\d+)Bytes/,'$1')
                if(offset){
                    offset = parseInt(offset,10)
                    console.log(`获取 ${signature} offset成功： ${offset}`);
                }
            }

            FormatHandler.add(ext, signature, desc).setOffset(signature,offset)

        }
    }

    console.log("解析HTML完成")





    console.log("正在写入数据")

    FormatHandler.write()

    console.log("写入数据完毕")
}


